<?php

class ReseauController extends Zend_Controller_Action
{

	public function init()
	{
	
		$this->view->addHelperPath(
		'ZendX/JQuery/View/Helper', 
		'ZendX_JQuery_View_Helper');
	}

	public function indexAction()
	{
		// action body
	}

	public function addAction()
	{
		$form = new Application_Form_Reseau();
		$envoyer = new Zend_Form_Element_Submit('envoyer');
		$envoyer->setAttrib('id', 'boutonenvoyer');
		$form->addElements(array($envoyer));
		$form->envoyer->setLabel('Ajouter');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
					
				$nom=$form->getValue('nom');
				$raccourci=$form->getValue('raccourci');

				$reseaux = new Application_Model_DbTable_Reseaux();
				$reseaux->addReseau($nom,$raccourci);

				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}
	}

	public function updateAction()
	{
		$form = new Application_Form_Reseau();
		$envoyer = new Zend_Form_Element_Submit('envoyer');
		$envoyer->setAttrib('id', 'boutonenvoyer');
		$form->addElements(array($envoyer));
		$form->envoyer->setLabel('Mettre a jour');
		$this->view->form = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$id= $form->getValue('id');
				$nom=$form->getValue('nom');
				$raccourci=$form->getValue('raccourci');
				$reseaux = new Application_Model_DbTable_Reseaux();
				$reseaux->updateReseau($id,$nom,$raccourci);
				$this->_helper->redirector('list');

			} else {
				$form->populate($formData);
			}
		} else {
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$reseaux = new Application_Model_DbTable_Reseaux();
				$form->populate($reseaux->getReseau($id));
			}
		}


	}

	public function listAction(){
		$reseaux=new Application_Model_DbTable_Reseaux();
		$this->view->list=$reseaux->fetchAll();
	}

}

